package com.zxc.service.wechat.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxc.common.dto.RegisterDto;
import com.zxc.common.dto.WechatAuthExecution;
import com.zxc.common.enums.NumberEnum;
import com.zxc.common.enums.wechat.WechatAuthStateEnum;
import com.zxc.common.exceptions.WechatAuthOperationException;
import com.zxc.entity.wechat.PersonInfo;
import com.zxc.entity.wechat.WechatAuth;
import com.zxc.mapper.UserMapper;
import com.zxc.mapper.WechatAuthMapper;
import com.zxc.service.wechat.WechatAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class WechatAuthServiceImpl extends ServiceImpl<WechatAuthMapper, PersonInfo> implements WechatAuthService {

    @Autowired
    private WechatAuthMapper wechatAuthMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public WechatAuth findWechatAuthByOpenId(String openId) {
        return null;
    }

    /**
     * 微信注册
     *
     * @param wechatAuth 微信注册信息
     * @return 返回值
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public WechatAuthExecution register(WechatAuth wechatAuth) {
        //空值判断
        if (wechatAuth == null || wechatAuth.getOpenId() == null) {
            return new WechatAuthExecution(WechatAuthStateEnum.NULL_AUTH_INFO);
        }
        //0、根据微信用户的openId查询是否已经存在系统微信用户表中
        PersonInfo person = wechatAuthMapper.getPersonInfoByOpenId(wechatAuth.getOpenId());
        System.out.println("openId="+wechatAuth.getOpenId());
        System.out.println("查询到的用户信息======" + person);
        if (ObjectUtils.isNotEmpty(person)) {
            return new WechatAuthExecution(WechatAuthStateEnum.SUCCESS, wechatAuth);
        }
        //1、首先根据微信返回信息，在我们自己的用户表里面注册信息
        RegisterDto registerDto = new RegisterDto();
        registerDto.setAvatar(wechatAuth.getPersonInfo().getProfileImg());
        registerDto.setStatus(NumberEnum.ZERO.getKey());
        registerDto.setUserName(wechatAuth.getPersonInfo().getName());
        registerDto.setPassword(SecureUtil.md5(NumberEnum.ONE_TO_SIX.getValue()));
        registerDto.setEmail(wechatAuth.getPersonInfo().getEmail());
        Integer register = userMapper.register(registerDto);
        if (register > NumberEnum.ZERO.getKey()) {
            System.out.println("系统注册成功了");
            //2、注册微信关联信息
            PersonInfo personInfo = wechatAuth.getPersonInfo();
            personInfo.setOpenId(wechatAuth.getOpenId());
            personInfo.setEnableStatus(NumberEnum.ONE.getKey());
            Integer result = wechatAuthMapper.register(personInfo);
            if (result < NumberEnum.ZERO.getKey()) {
                throw new WechatAuthOperationException("添加用户信息失败");
            }
            System.out.println("系统微信表添加成功！");
        }
        return new WechatAuthExecution(WechatAuthStateEnum.SUCCESS, wechatAuth);
    }
}
